package com.niuwa.p2p.service.impl.sms;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.jianzhou.sdk.BusinessService;
import com.niuwa.p2p.core.constant.SmsCheckEnum;
import com.niuwa.p2p.core.constant.SmsSignEnum;
import com.niuwa.p2p.core.constant.SmsStatusEnum;
import com.niuwa.p2p.core.utils.MobileUtil;
import com.niuwa.p2p.service.sms.SmsService;

/**
 * 短信发送接口实现类
 * 
 * @author bwz
 * @update by tennyqin
 *
 */
@Service
public class SmsServiceImpl implements SmsService {

	@Value("#{propertiesReader['system.sms.wsdl']}")
	private String wsdl;

	@Value("#{propertiesReader['system.sms.account']}")
	private String account;

	@Value("#{propertiesReader['system.sms.password']}")
	private String password;

	@Value("#{propertiesReader['system.sms.certificateLocation']}")
	private String certificateLocation;

	@Value("#{propertiesReader['system.sms.secretpassword']}")
	private String secretpassword;

	@Value("#{propertiesReader['system.sms.sign']}")
	private String sign;

	/**
	 * 提交短信
	 */
	@Override
	public SmsStatusEnum sendBatchMessage(String destMobiles, String contents) {
		if (SmsCheckEnum.Success != checkAccount()) {
			return SmsStatusEnum.ErrorAccount;
		}
		if (!MobileUtil.isInludeSign(contents, sign, SmsSignEnum.Nomal)) {
			return SmsStatusEnum.ErrorSign;
		}
		System.setProperty("javax.net.ssl.trustStore", certificateLocation);
		System.setProperty("javax.net.ssl.trustStorePassword", secretpassword);

		BusinessService bs = new BusinessService();
		bs.setWebService(wsdl);

		int result = bs.sendBatchMessage(account, password, destMobiles,
				contents);
		if (result > 0) {
			return SmsStatusEnum.Success;
		}
		return SmsStatusEnum.valueOf(result);
	}

	/**
	 * 定时批量发送短信
	 */

	@Override
	public SmsStatusEnum sendTimelyMessage(String sendDateTime,
			String destMobiles, String contents) {
		if (SmsCheckEnum.Success != checkAccount()) {
			return SmsStatusEnum.ErrorAccount;
		}
		if (!MobileUtil.isInludeSign(contents, sign, SmsSignEnum.Nomal)) {
			return SmsStatusEnum.ErrorSign;
		}

		System.setProperty("javax.net.ssl.trustStore", certificateLocation);
		System.setProperty("javax.net.ssl.trustStorePassword", secretpassword);

		BusinessService bs = new BusinessService();
		bs.setWebService(wsdl);

		int result = bs.sendTimelyMessage(account, password, sendDateTime,
				destMobiles, contents);
		if (result > 0) {
			return SmsStatusEnum.Success;
		}

		return SmsStatusEnum.valueOf(result);
	}

	/**
	 * 批量发送个性化短信
	 */
	@Override
	public SmsStatusEnum sendPersonalMessages(String destMobiles,
			String contents) {
		if (SmsCheckEnum.Success != checkAccount()) {
			return SmsStatusEnum.ErrorAccount;
		}
		if (!MobileUtil.isPersonalSms(destMobiles, contents)) {
			return SmsStatusEnum.Other;
		}
		if (!MobileUtil.isInludeSign(contents, sign, SmsSignEnum.Personal)) {
			return SmsStatusEnum.ErrorSign;
		}

		System.setProperty("javax.net.ssl.trustStore", certificateLocation);
		System.setProperty("javax.net.ssl.trustStorePassword", secretpassword);

		BusinessService bs = new BusinessService();
		bs.setWebService(wsdl);

		int result = bs.sendPersonalMessages(account, password, destMobiles,
				contents);
		if (result > 0) {
			return SmsStatusEnum.Success;
		}
		if (result == -15) { // 特殊情况接口未定义
			return SmsStatusEnum.Other;
		}

		return SmsStatusEnum.valueOf(result);
	}

	/**
	 * 语音短信
	 */

	@Override
	public SmsStatusEnum sendAudio(String destMobiles, String sendDateTime,
			String contents) {
		if (SmsCheckEnum.Success != checkAccount()) {
			return SmsStatusEnum.ErrorAccount;
		}
		if (!MobileUtil.isInludeSign(contents, sign, SmsSignEnum.Nomal)) {
			return SmsStatusEnum.ErrorSign;
		}
		int resultValue = 0;
		System.setProperty("javax.net.ssl.trustStore", certificateLocation);
		System.setProperty("javax.net.ssl.trustStorePassword", secretpassword);

		BusinessService bs = new BusinessService();
		bs.setWebService(wsdl);

		String result = bs.sendAudio(account, password, sendDateTime,
				destMobiles, contents, "");
		if (!MobileUtil.isEmpty(result)) {
			resultValue = Integer.valueOf(result);
		}
		if (resultValue > 0) {
			return SmsStatusEnum.Success;
		}

		return SmsStatusEnum.valueOf(resultValue);
	}

	/**
	 * 服务器监控短信发送接口 短信签名不需要增加，程序处理为自动增加
	 */

	@Override
	public SmsStatusEnum sendServierMoniter(String destMobiles, String contents) {
		if (SmsCheckEnum.Success != checkAccount()) {
			return SmsStatusEnum.ErrorAccount;
		}
		System.setProperty("javax.net.ssl.trustStore", certificateLocation);
		System.setProperty("javax.net.ssl.trustStorePassword", secretpassword);

		BusinessService bs = new BusinessService();
		bs.setWebService(wsdl);
		// 服务器linux无法书写中文,则自动增加短信签名
		StringBuffer contentBuffer = new StringBuffer();
		contentBuffer.append(contents);
		contentBuffer.append(sign);
		if (!MobileUtil.isInludeSign(String.valueOf(contentBuffer), sign,
				SmsSignEnum.Nomal)) {
			return SmsStatusEnum.ErrorSign;
		}
		int result = bs.sendBatchMessage(account, password, destMobiles,
				String.valueOf(contentBuffer));
		if (result > 0) {
			return SmsStatusEnum.Success;
		}
		return SmsStatusEnum.valueOf(result);

	}

	/**
	 * 校验发送账号的合法性
	 */
	private SmsCheckEnum checkAccount() {
		System.setProperty("javax.net.ssl.trustStore", certificateLocation);
		System.setProperty("javax.net.ssl.trustStorePassword", secretpassword);
		BusinessService bs = new BusinessService();
		bs.setWebService(wsdl);
		int checkResult = bs.validateUser(account, password);
		return SmsCheckEnum.valueOf(checkResult);
	}
}
